﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling.Shell;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace VsxFactory.Modeling
{
    /// <summary>
    /// 
    /// </summary>
    public static class StoreExtensions
    {
        /// <summary>
        /// Gets the service.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="store">The store.</param>
        /// <returns></returns>
        public static T GetService<T>(this Microsoft.VisualStudio.Modeling.Store store) where T : class
        {
            return store.GetService(typeof(T)) as T;
        }

        /// <summary>
        /// Gets the service.
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="store">The store.</param>
        /// <returns></returns>
        public static T GetService<S, T>(this Microsoft.VisualStudio.Modeling.Store store)
            where T : class
            where S : class
        {
            return store.GetService(typeof(S)) as T;
        }

        /// <summary>
        /// Currents the doc data.
        /// </summary>
        /// <param name="store">The store.</param>
        /// <returns></returns>
        public static ModelingDocData CurrentDocData(this Microsoft.VisualStudio.Modeling.Store store)
        {
            foreach (Diagram diagram in store.ElementDirectory.FindElements<Diagram>())
            {
                VSDiagramView diagramView = diagram.ActiveDiagramView as VSDiagramView;
                if (diagramView != null)
                {
                    ModelingDocData docData = diagramView.DocData as ModelingDocData;
                    if (docData != null)
                        return docData;
                }
            }
            return null;
        }

        /// <summary>
        /// Opens the view.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="store">The store.</param>
        /// <param name="logicalView">The logical view.</param>
        /// <param name="viewContext">The view context.</param>
        public static void OpenView<T>(this Microsoft.VisualStudio.Modeling.Store store, Guid logicalView, object viewContext)
        {
            var docData = CurrentDocData(store);
            if (docData != null)
                docData.OpenView(logicalView, viewContext);
        }


        /// <summary>
        /// Gets the name of the file.
        /// </summary>
        /// <param name="store">The store.</param>
        /// <returns></returns>
        public static string GetFileName(this Microsoft.VisualStudio.Modeling.Store store)
        {
            foreach (Diagram diagram in store.ElementDirectory.FindElements<Diagram>(true))
            {
                VSDiagramView view = diagram.ActiveDiagramView as VSDiagramView;
                if (view != null)
                {
                    return view.DocData.FileName;
                }
            }

            return string.Empty;
        }
    }
}
